package com.qujia.markert.service.impl;

import com.qujia.common.core.domain.AjaxResult;
import com.qujia.markert.domain.dto.CouponReductionDto;
import com.qujia.markert.domain.vo.CouponUserDto;
import com.qujia.markert.domain.vo.CouponUserVo;
import com.qujia.markert.domain.vo.DiscountVo;
import com.qujia.markert.domain.vo.ReductionDisVo;
import com.qujia.markert.service.ActivitySyncService;
import com.qujia.markert.service.IMarketCouponService;
import com.qujia.markert.service.IReductionActivityService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class ActivitySyncServiceImpl implements ActivitySyncService {

    @Resource
    private IReductionActivityService reductionActivityService;

    @Resource
    private IMarketCouponService marketCouponService;

    @Override
    public AjaxResult selectDiscount(CouponReductionDto couponReductionDto) {
        ReductionDisVo reductionDisVo = reductionActivityService.selectReductionAmt(couponReductionDto.getAmt(),couponReductionDto.getStationId(),couponReductionDto.getOilName(),
                couponReductionDto.getOilLiters(),couponReductionDto.getMobilePhone());

        //查询用户优惠券
        List<CouponUserVo> couponList = marketCouponService.getCouponList(couponReductionDto.getMobilePhone(), couponReductionDto.getOilName(),
                couponReductionDto.getStationId(), new BigDecimal(couponReductionDto.getAmt()), reductionDisVo.getDiscountAmt(), reductionDisVo.getActivityId());

        List<CouponUserDto> couponUserDtos = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(couponList)){
            couponUserDtos = couponList.stream().map(m -> {

                CouponUserDto couponUserDto = new CouponUserDto();
                couponUserDto.setCouponUserId(m.getCouponUserId());
                couponUserDto.setCouponType(Integer.valueOf(m.getCouponType()));
                couponUserDto.setCouponAmt(m.getCouponAmt());
                return couponUserDto;
            }).collect(Collectors.toList());

        }

        CouponUserDto maxCouponUserIdAndAmt = new CouponUserDto();
        //优惠券信息
        if(CollectionUtils.isNotEmpty(couponUserDtos)){
            maxCouponUserIdAndAmt = marketCouponService.getMaxCouponUserIdAndAmt(couponList, new BigDecimal(couponReductionDto.getAmt()));
        }

        BigDecimal reductionAmt = Objects.isNull(reductionDisVo.getDiscountAmt()) ? BigDecimal.ZERO : reductionDisVo.getDiscountAmt();

        DiscountVo discountVo = new DiscountVo();
        discountVo.setReductionAmt(reductionAmt);
        discountVo.setCouponUserList(couponUserDtos);
        discountVo.setUseCouponUserId(maxCouponUserIdAndAmt.getCouponUserId());
        BigDecimal couponAmt =  Objects.isNull(maxCouponUserIdAndAmt.getCouponAmt()) ? BigDecimal.ZERO : maxCouponUserIdAndAmt.getCouponAmt();

        discountVo.setCouponDiscountAmt(couponAmt);

        discountVo.setTotalDiscount(reductionAmt.add(couponAmt));
        return AjaxResult.success(discountVo);
    }
}
